package com.simba.server.commands.impl.fetch;

import java.sql.ResultSet;
import com.simba.common.command.ICommand;
import com.simba.common.database.DbOperator;
import com.simba.server.components.data.MachineRunInfo;

public class FetchPackageOffCountCmd implements ICommand
{
	public static final String NAME = "FetchPackageOffCountCmd";
	private MachineRunInfo info;
	private final String sql = "select count(id) as count from tt_packet_off_info where recordTime >= ? and recordTime <= ?";

	public FetchPackageOffCountCmd(MachineRunInfo info)
	{
		this.info = info;
	}

	@Override
	public String getName()
	{
		return FetchPackageOffCountCmd.NAME;
	}

	@Override
	public Boolean run(Object params)
	{
		boolean ret = false;

		if (!(params instanceof DbOperator))
		{
			return ret;
		}

		try
		{
			DbOperator dbOperator = (DbOperator) params;

			dbOperator.prepareStatement(this.sql);
			dbOperator.setString(1, info.getBeginTime());
			dbOperator.setString(2, info.getEndTime());

			ResultSet rs = dbOperator.executeQuery();
			if (rs.next())
			{
				info.setPackageCount(rs.getInt("count"));
			}
			else
			{
				info.setPackageCount(0);
			}

			ret = true;
		}
		catch (Exception ex)
		{
			ret = false;
		}

		return ret;
	}
}
